package com.phone580.app.sysapp;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.phone580.app.BaseActivity;
import com.phone580.app.R;
import com.phone580.app.entity.AppInfoEntity;
import com.phone580.app.untilty.CommonUtility;

/**
 * 系统自带软件，需要root
 * @author xianpunan
 * @date     2012-7-27
 */
public class SysAppActivity extends BaseActivity {

	private ListView lvAppList;
	private ProgressBar pbLoad;
	private SysAppAdapter adapter;
	private List<AppInfoEntity> data;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			adapter.setData(data);
			lvAppList.setAdapter(adapter);
			pbLoad.setVisibility(View.GONE);
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sys_app_layout);
		initHeader("系统软件");
		//判断是否root
		if (!CommonUtility.isRooted()) {
			Toast.makeText(this, "无法获取root权限", Toast.LENGTH_LONG).show();
			finish();
			return;
		}
		lvAppList = (ListView) findViewById(R.id.sys_app_list);
		pbLoad = (ProgressBar) findViewById(R.id.sys_app_load_pb);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		refresh();
	}
	//刷新数据
	public void refresh() {
		pbLoad.setVisibility(View.VISIBLE);
		Thread t = new WorkerThread();
		adapter = new SysAppAdapter(this);
		adapter.setData(new ArrayList<AppInfoEntity>());
		t.start();
	}
	
	//工作线程
	class WorkerThread extends Thread {
		@Override
		public void run() {
//			List<PackageInfo> apps = new ArrayList<PackageInfo>();
//			PackageManager pManager = getPackageManager();
//			// 获取手机内所有应用
//			List<PackageInfo> paklist = pManager.getInstalledPackages(0);
//			for (int i = 0; i < paklist.size(); i++) {
//				PackageInfo pak = (PackageInfo) paklist.get(i);
//				// 判断是否为非系统预装的应用程序
//				if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
//					// system applications
//					apps.add(pak);
//				}
//			}
//
//			data = new ArrayList<AppInfoEntity>();
//			for (PackageInfo pkg : apps) {
//				String label = pManager.getApplicationLabel(pkg.applicationInfo)
//						.toString();
//				String pkgName = pkg.packageName;
//				String version = pkg.versionName;
//				Drawable icon = pManager.getApplicationIcon(pkg.applicationInfo);
//				String srcPath = pkg.applicationInfo.sourceDir;
//				System.out.println(srcPath);
//				File file = new File(srcPath);
//				long size = file.length();
//				AppInfoEntity info = new AppInfoEntity();
//				info.setAppLabel(label);
//				info.setIcon(icon);
//				info.setPkgName(pkgName);
//				info.setVersion(version);
//				info.setSize(size);
//				info.setApkPath(srcPath);
//				data.add(info);
//			}
			data = CommonUtility.getSysAppList(SysAppActivity.this);
			handler.sendEmptyMessage(0);
		}
	}
}
